<!-- 折线堆叠图 -->
<template>
    <div class="wrap" ref="lineRef"></div>
</template>

<script lang='ts' setup>
import { markRaw, onMounted, onUnmounted, ref } from 'vue';
import * as echarts from 'echarts/core';
import {
    TitleComponent,
    TitleComponentOption,
    ToolboxComponent,
    ToolboxComponentOption,
    TooltipComponent,
    TooltipComponentOption,
    GridComponent,
    GridComponentOption,
    LegendComponent,
    LegendComponentOption
} from 'echarts/components';
import { LineChart, LineSeriesOption } from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import { handleResize } from '@/utils/other';

echarts.use([
    TitleComponent,
    ToolboxComponent,
    TooltipComponent,
    GridComponent,
    LegendComponent,
    LineChart,
    CanvasRenderer,
    UniversalTransition
]);

type EChartsOption = echarts.ComposeOption<
    | TitleComponentOption
    | ToolboxComponentOption
    | TooltipComponentOption
    | GridComponentOption
    | LegendComponentOption
    | LineSeriesOption
>;

let lineRef = ref()
let chart = ref()
let flag = ref<boolean>(true)
const option: EChartsOption = {
    title: {
        text: '周热度'
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            label: {
                backgroundColor: '#6a7985'
            }
        }
    },
    legend: {
        data: ['马保国', '嘎子', '潘子', '二营长', '鸡你太美']
    },
    toolbox: {
        feature: {
            // saveAsImage: {}
        }
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: [
        {
            type: 'category',
            boundaryGap: false,
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        }
    ],
    yAxis: [
        {
            type: 'value'
        }
    ],
    series: [
        {
            name: '马保国',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
                focus: 'series'
            },
            data: [120, 300, 600, 200, 300, 230, 210]
        },
        {
            name: '嘎子',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
                focus: 'series'
            },
            data: [220, 182, 191, 234, 290, 330, 310]
        },
        {
            name: '潘子',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
                focus: 'series'
            },
            data: [150, 232, 201, 154, 190, 330, 410]
        },
        {
            name: '二营长',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
                focus: 'series'
            },
            data: [320, 332, 301, 334, 390, 330, 320]
        },
        {
            name: '鸡你太美',
            type: 'line',
            stack: 'Total',
            label: {
                show: true,
                position: 'top'
            },
            areaStyle: {},
            emphasis: {
                focus: 'series'
            },
            data: [400, 800, 600, 200, 242, 654, 323]
        }
    ]
}
onMounted(() => {

    chart.value = markRaw(echarts.init(lineRef.value))
    chart.value.setOption(option)

    window.addEventListener('resize', handle)
    chart.value.on('rendered',()=>{
        if (!flag.value) return
        flag.value = false
        setTimeout(() => {
            chart.value.resize()
            handleResize()
        }, 1000)
    })
})
const handle = () => {
    chart.value.resize()
}
onUnmounted(() => {
    window.removeEventListener('resize', handle)
})

</script>

<style scoped>
.wrap {
   overflow: hidden;
}
</style>